package com.lijiajian.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijiajian.base.BaseService;
import com.lijiajian.dao.CustomerLossMapper;
import com.lijiajian.pojo.CusDevPlan;
import com.lijiajian.pojo.CustomerLoss;
import com.lijiajian.query.CusDevPlanQuery;
import com.lijiajian.query.CustomerLossQuery;
import com.lijiajian.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CustomerLossService extends BaseService<CustomerLoss,Integer> {
    @Resource
    private CustomerLossMapper customerLossMapper;

    /**
     * 多条件查询流失客户
     * @param customerLossQuery
     * @return
     */
    public Map<String,Object> queryCustomerLossByParams(CustomerLossQuery customerLossQuery){


            Map<String,Object> map=new HashMap<>();
            //开启分页
            PageHelper.startPage(customerLossQuery.getPage(),customerLossQuery.getLimit());
            PageInfo<CustomerLoss> pageInfo=new PageInfo<>(customerLossMapper.selectByParams(customerLossQuery));
            map.put("code",0);
            map.put("msg","success");
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());

            return map;

    }

    /**
     * 确认客户流失
     * @param id
     * @param lossReason
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCustomerLossState(Integer id,String lossReason){
        AssertUtil.isTrue(id==null,"待确认流失的客户不存在");
        CustomerLoss customerLoss = customerLossMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null==customerLoss,"待确认流失的客户不存在");
        AssertUtil.isTrue(StringUtils.isBlank(lossReason),"流失原因为空");
        customerLoss.setLossReason(lossReason);
        customerLoss.setState(1);
        customerLoss.setConfirmLossTime(new Date());

        customerLoss.setUpdateDate(new Date());

        AssertUtil.isTrue(customerLossMapper.updateByPrimaryKeySelective(customerLoss)!=1,"确认流失客户失败");
    }
}
